﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;

namespace FGrubert.CrmQueueMonitor.WinForms
{
    /// <summary>
    /// Represents a cell that is shown a status as a traffic light
    /// </summary>
    public class DataGridViewStatusCell : DataGridViewCell
    {
        private const int HORIZONTALOFFSET = 3;
        private const int VERTICALOFFSET = 3;


        /// <summary>
        /// Paint the traffice light status
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="clipBounds"></param>
        /// <param name="cellBounds"></param>
        /// <param name="rowIndex"></param>
        /// <param name="cellState"></param>
        /// <param name="value"></param>
        /// <param name="formattedValue"></param>
        /// <param name="errorText"></param>
        /// <param name="cellStyle"></param>
        /// <param name="advancedBorderStyle"></param>
        /// <param name="paintParts"></param>
        protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex,
            DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle,
            DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

            int diameter = Math.Min(cellBounds.Width - (HORIZONTALOFFSET * 2), cellBounds.Height - (VERTICALOFFSET * 2));
            Color color = Color.LightGray;
            // Get the parent column and the maximum value:
            DataGridViewStatusColumn parent = (DataGridViewStatusColumn)this.OwningColumn;

            if (!Convert.IsDBNull(value))
            {
                QueueStatus status = (QueueStatus)value;

                switch (status)
                {
                    case QueueStatus.Green:
                        color = Color.FromArgb(0, 255, 33);
                        break;
                    case QueueStatus.Yellow:
                        color = Color.FromArgb(255, 216, 0);
                        break;
                    case QueueStatus.Red:
                        color = Color.FromArgb(255, 0, 0);
                        break;
                    default:
                        color = Color.LightGray;
                        break;
                }
            }

            // Paint the background
            Brush backgroundBrush = new SolidBrush(parent.InheritedStyle.BackColor);
            if ((cellState & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected)
            {
                backgroundBrush = new SolidBrush(parent.InheritedStyle.SelectionBackColor);
            }
            graphics.FillRectangle(backgroundBrush, cellBounds);

            // Paint the foreground
            System.Drawing.Drawing2D.SmoothingMode oldSmoothingMode = graphics.SmoothingMode;
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            //Inner color
            RectangleF lightRect = new RectangleF(cellBounds.X + (cellBounds.Width / 2) - (diameter / 2), cellBounds.Y + (cellBounds.Height / 2) - (diameter / 2), diameter, diameter);
            Brush brush = new SolidBrush(color);
            graphics.FillEllipse(brush, lightRect);
            // Outer color
            Pen pen = new Pen(ControlPaint.Dark(color), 3);
            graphics.DrawEllipse(pen, lightRect);
            // Reset old smoothing mode to prevent display errors
            graphics.SmoothingMode = oldSmoothingMode;
        }
    }
}
